﻿using System.ComponentModel;
using System.Linq;
using System.Windows;
using Fayshine.VA.Helpers;
using Fayshine.VA.RIAService.Web;
using Fayshine.VA.Controls;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace Fayshine.VA.ViewModels
{
    public class ShopSettingViewModel : INotifyPropertyChanged
    {
        #region Private Fields

        private FayshineDomainContext m_Context = null;

        #endregion

        public ShopSettingViewModel()
        {
            m_Context = new FayshineDomainContext();
            this.Areas = new ObservableCollection<area>();
            this.SelectedGates = new ObservableCollection<gate>();
            this.EffectGates = new ObservableCollection<gate>();
            this.Gates = new ObservableCollection<gate>();
            this.Cameras = new ObservableCollection<camera>();
            this.SelectedCameras = new ObservableCollection<camera>();
            this.EffectCameras = new ObservableCollection<camera>();
        }

        public ObservableCollection<area> Areas
        {
            get;
            set;
        }

        public ObservableCollection<gate> SelectedGates
        {
            get;
            set;
        }

        public ObservableCollection<gate> EffectGates
        {
            get;
            set;
        }

        public ObservableCollection<gate> Gates
        {
            get;
            set;
        }

        public ObservableCollection<camera> Cameras
        {
            get;
            set;
        }

        public ObservableCollection<camera> SelectedCameras
        {
            get;
            set;
        }

        public ObservableCollection<camera> EffectCameras
        {
            get;
            set;
        }

        #region Public Methods

        public void GetAllAreas()
        {
            var getAreasQuery = m_Context.GetAreasQuery();
            var loadShopsOperation = m_Context.Load(getAreasQuery, (s) =>
            {
                if (!s.HasError)
                {
                    s.Entities.ToList().ForEach(e =>
                    {
                        this.Areas.Add(e);
                    });
                }
                else
                {
                    MessageBox.Show(s.Error.Message + s.Error.StackTrace);
                }
                BusyIndicator indicator = Application.Current.RootVisual as BusyIndicator;
                indicator.IsBusy = false;
            }, true);
        }

        public void AddArea(area area)
        {
            this.Areas.Add(area);
            this.m_Context.areas.Add(area);
            SaveData();
        }

        public void Update(area device)
        {
            SaveData();
        }

        public void DeleteShop(area area)
        {
            area.gates.each(d => {

                d.ParentID = null;
                
                this.SelectedGates.Remove(d);
                if(!this.EffectGates.Contains(d))
                {
                  this.EffectGates.Add(d);
                }
            
            });

            //area.pos.each(p => {
            //    this.m_Context.pos.Remove(p);
            //});

            this.Areas.Remove(area);
            this.m_Context.areas.Remove(area);
            SaveData();
        }

        public void DeleteDevices(List<area> areas)
        {
            areas.each(u =>
            {

                u.gates.each(d =>
                {

                    d.ParentID = null;

                    this.SelectedGates.Remove(d);
                    if (!this.EffectGates.Contains(d))
                    {
                        this.EffectGates.Add(d);
                    }
            


                });
                //u.pos.each(p =>
                //{
                //    this.m_Context.pos.Remove(p);
                //});
                this.Areas.Remove(u);
                this.m_Context.areas.Remove(u);
            });
            SaveData();
        }

        public void GetAllGatesIncludeArea()
        {
            var getGateQuery = m_Context.GetGatesQuery();
            var loadShopsOperation = m_Context.Load(getGateQuery, (s) =>
            {
                if (!s.HasError)
                {
                    s.Entities.ToList().ForEach(e =>
                    {
                        this.Gates.Add(e);
                        if (e.ParentID == null || e.ParentID.Value < 0)
                        {
                            this.EffectGates.Add(e);
                        }
                    });
                    GetAllAreas();
                }
                else
                {
                    MessageBox.Show(s.Error.Message + s.Error.StackTrace);
                }
                //BusyIndicator indicator = Application.Current.RootVisual as BusyIndicator;
                //indicator.IsBusy = false;
            }, true);
        }

        public void AddGate(gate gate)
        {
            this.Gates.Add(gate);
            this.SelectedGates.Add(gate);
            this.m_Context.gates.Add(gate);
            SaveData();
        }

        public void Update(gate device)
        {
            SaveData();
        }

        public void RemoveDoor(gate gate)
        {
            this.SelectedGates.Remove(gate);
            this.EffectGates.Add(gate);
            SaveData();
        }

        public void AppendGate(gate gate)
        {
            this.EffectGates.Remove(gate);
            this.SelectedGates.Add(gate);
            SaveData();
        }

        public void DeleteGate(gate gate)
        {
            gate.cameras.each(e => {

                e.AreaID = null;
                this.SelectedCameras.Remove(e);
                if (!this.EffectCameras.Contains(e))
                {
                    this.EffectCameras.Add(e);
                }
            
            });

            this.Gates.Remove(gate);
            this.SelectedGates.Remove(gate);
            this.EffectGates.Remove(gate);
            this.m_Context.gates.Remove(gate);
            SaveData();
        }

        public void DeleteDevices(List<gate> gates)
        {
            gates.each(u =>
            {

                u.cameras.each(e =>
                {

                    e.AreaID = null;
                    this.SelectedCameras.Remove(e);
                    if (!this.EffectCameras.Contains(e))
                    {
                        this.EffectCameras.Add(e);
                    }

                });
                this.Gates.Remove(u);
                this.SelectedGates.Remove(u);
                this.EffectGates.Remove(u);
                this.m_Context.gates.Remove(u);
            });
            SaveData();
        }

        public void GetAllCamerasIncludeGate()
        {
            var getCameraQuery = m_Context.GetCamerasQuery();
            var loadShopsOperation = m_Context.Load(getCameraQuery, (s) =>
            {
                if (!s.HasError)
                {
                    s.Entities.ToList().ForEach(e =>
                    {
                        this.Cameras.Add(e);
                        if (e.AreaID == null || e.AreaID.Value < 0)
                        {
                            this.EffectCameras.Add(e);
                        }
                    });
                    GetAllGatesIncludeArea();
                }
                else
                {
                    MessageBox.Show(s.Error.Message + s.Error.StackTrace);
                }
                //BusyIndicator indicator = Application.Current.RootVisual as BusyIndicator;
                //indicator.IsBusy = false;
            }, true);
        }

        public void AppendCamera(camera camera)
        {
            this.EffectCameras.Remove(camera);
            this.SelectedCameras.Add(camera);
            SaveData();
        }

        public void RemoveCamera(camera camera)
        {
            this.SelectedCameras.Remove(camera);
            this.EffectCameras.Add(camera);
            SaveData();
        }

        public void SaveData()
        {
            if (this.m_Context.HasChanges)
            {
                this.m_Context.SubmitChanges((s) =>
                {

                    //BusyIndicator indicator = Application.Current.RootVisual as BusyIndicator;

                    //indicator.IsBusy = false;
                    if (s.HasError)
                    {
                        MessageBox.Show("Error occurs,Save Failed!");
                    }

                }, null);
            }
        }

        public void SaveData2()
        {
            if (this.m_Context.HasChanges)
            {
                this.m_Context.SubmitChanges((s) =>
                {

                    //BusyIndicator indicator = Application.Current.RootVisual as BusyIndicator;

                    //indicator.IsBusy = false;
                    if (s.HasError)
                    {
                        MessageBox.Show("Error occurs,Save Failed!");
                    }
                    else
                    {
                        MessageBox.Show("保存成功！");
                    }

                }, null);
            }
        }

        public bool ExistShop(string title)
        {
            return this.Areas.Where(u => u.Name.Equals(title, System.StringComparison.OrdinalIgnoreCase)).Any();
        }

        public bool ExistDoor(string title)
        {
            return this.Gates.Where(u => u.Name.Equals(title, System.StringComparison.OrdinalIgnoreCase)).Any();
        }

        public void UpdateSelectArea(int areaID)
        {
            this.SelectedGates.Clear();
            this.Gates.each(d => {
                if (d.ParentID == areaID)
                {
                    this.SelectedGates.Add(d);
                }
            });
        }

        public void UpdateSelectGate(int gateID)
        {
            this.SelectedCameras.Clear();
            this.Cameras.each(c => {
                if (c.AreaID == gateID)
                {
                    this.SelectedCameras.Add(c);
                }
            
            });
        }

        #endregion


        #region INotifyPropertyChanged Interface

        public event PropertyChangedEventHandler PropertyChanged;
        public void NofityPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion
    }
}
